<?

	function microtime_float()
{
    list($utime, $time) = explode(" ", microtime());
    return ((float)$utime + (float)$time);
}
	

# Class to manage connections
class Database
{
	# Config
	private $Database;
	private $Host;
	private $TableName;
	private $Username;
	private $Password;
	
	private $Connections;
	private $Areas;
	
	private $Connected;
	private $Killed;
	
	public function Database($Host, $TableName, $Username, $Password)
	{
		// Set Data
		$this->Host 	= $Host;
		$this->TableName = $TableName;
		$this->Username = $Username;
		$this->Password = $Password;
		
		// Connect
		$this->Connect();
	}
	
	// Test Function
	public function Test()
	{
		echo 'PDO is working!';
	}
	
	// Connect to database
	private function Connect()
	{
		$this->Database = new PDO('mysql:host='. $this->Host .';dbname='. $this->TableName .'', $this->Username, $this->Password);	
	}
	
	// Disconnect from database
	public function Disconnect()
	{
		$this->Killed = microtime_float();
		Show("Total Connections: ". $this->Connections);
		Show("Connected: ". $this->Connected);
		Show("Killed: ". $this->Killed);	
		Show("Alive: ". ($this->Killed - $this->Connected));	
		Show($this->Areas);
		
		$this->Database = NULL;
	}
	
	#------------------------------------------------------------------------------------------------------------------#
	# Get Methods																									   #
	#------------------------------------------------------------------------------------------------------------------#
	
	/* Method to get data, returns array(Size, Errors, Columns, ArrayData)
		If an error has occured, Errors will have 3 values (instead of 1), value 2 will be message
		
		$Table			The table you want data from
		$Fields			Specific fields you want, can accept array or use * for all
		$All			Weather you want all results or just the first one found
		$Where			Where conditions, NULL by default, Can accept an array of conditions
		$Order			The order, ASC/DESC , ASC by default
		$Limit			limit, input as an array(Start, Length), default no limit
	*/

	
	private $LastValue = 0;
	public function GetData($Table, $Fields, $All, $Where = NULL, $Order = "AUTO", $Direction = "ASC", $Limit = NULL)
	{
		if ($this->Connections == 0)
			$this->Connected = microtime_float();
			
		$NewTime = microtime_float();
		$this->Areas[] = $Table .' > ('. count($Fields) .') '.  $All .' ['. $Where .'] > '. $Limit[1] .' == '. $NewTime .' / '. ($NewTime - $this->LastValue);
		$this->Connections++;
		$this->LastValue = $NewTime;
		
		// Create fields into comma string if an array
		if (is_array($Fields))
			$field_values = implode(",", $Fields);
		else
			$field_values = $Fields;
			
		// Set WHERE clause
		if (is_array($Where))
			$where_conditions = implode(" AND ", $Where);
		else
			$where_conditions = $Where;
		
		// If WHERE conditions set, create string condition	
		if (isset($where_conditions))
			$where_conditions = "WHERE ". $where_conditions;
			
		// Set Limit
		if (isset($Limit))
			$limit_conditions = "LIMIT ". implode(",", $Limit);
			
		//Show($limit_conditions);
		
		// Run Query
		$query = $this->Database->prepare("SELECT ". $field_values ." FROM ". $Table ." ". $where_conditions ." ORDER BY ". $Order ." ". $Direction ." ". $limit_conditions ."");
		$query->execute();
		
		// Get Results
		if ($All)
			$results = $query->fetchAll(PDO::FETCH_ASSOC);
		else
			$results = $query->fetch(PDO::FETCH_ASSOC);
		
		// Set Size
		$size = $query->rowCount();
		
		// Set Error
		$errors = $query->errorInfo();
		
		// Set Columns
		$columns = $query->columnCount();
		
		// Return
		return array($size, $errors, $columns, $results);
	}
	
	#------------------------------------------------------------------------------------------------------------------#
	# Insert Methods																								   #
	#------------------------------------------------------------------------------------------------------------------#
	
	public function Insert($Table, $Data)
	{
		$this->Connections++;
		#Show($Data);
		
		// Run Query
		$query = $this->Database->prepare("INSERT INTO ". $Table ." SET ". implode(",", $Data));
		
		#Show($query);
		
		$query->execute();

		// Set Error
		$errors = $query->errorInfo();
		
		// Return
		return $errors;
	}
	
	public function Update($Table, $Data, $Where)
	{
		$this->Connections++;
		// Run Query
		if (is_array($Where))
			$where_conditions = implode(" AND ", $Where);
		else
			$where_conditions = $Where;
		
		if (isset($where_conditions))
			$where_conditions = "WHERE ". $where_conditions;
		
		$query = $this->Database->prepare("UPDATE ". $Table ." SET ". implode(",", $Data) ." ". $where_conditions);
		$query->execute();

		// Set Error
		$errors = $query->errorInfo();
		
		// Return
		return $errors;
	}
	
	#------------------------------------------------------------------------------------------------------------------#
	# Remove Methods																								   #
	#------------------------------------------------------------------------------------------------------------------#
	
	public function Remove($Table, $Where)
	{
		$this->Connections++;
		// Run Query
		if (is_array($Where))
			$where_conditions = implode(" AND ", $Where);
		else
			$where_conditions = $Where;
		
		if (isset($where_conditions))
			$where_conditions = "WHERE ". $where_conditions;
		
		$query = $this->Database->prepare("DELETE FROM ". $Table ." ". $where_conditions);
		$query->execute();

		// Set Error
		$errors = $query->errorInfo();
		
		// Return
		return $errors;
	}
	
	#------------------------------------------------------------------------------------------------------------------#
	# RAW SQL																										   #
	#------------------------------------------------------------------------------------------------------------------#
	
	public function RawQuery($Query)
	{
		$this->Connections++;
		// Run Query
		$query = $this->Database->prepare($Query);
		$query->execute();

		// Set Error
		$errors = $query->errorInfo();
		
		// Results
		$results = $query->fetchAll(PDO::FETCH_ASSOC);
		
		//Size
		$size = $query->rowCount();
		
		// Return
		return array($size, $errors, $results);
	}
	
	#------------------------------------------------------------------------------------------------------------------#
	# ESCAPING																										   #
	#------------------------------------------------------------------------------------------------------------------#
	
	public function Quote($String)
	{
		// Returns escaped string
		return $this->quote($String);
		
	}
}	
?>